﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Web.Mvc;
using DB.SDFAuth;
using Nop.Admin;
using Nop.Services.Blogs;
using SDF.Core;
using SDFAuth.Models;

namespace SDFAuth.Web.Controllers.Account
{
    public class PcdController : Controller
    {

        public ICityService CityService { get; set; }
        

        [ChildActionOnly]
        public ActionResult PcdSelect(PcdModel model)
        {
            if (model ==null)
            {
                model = new PcdModel();
                model.ProvinceID =
                model.CityID =
                model.DistrictID = -1;
            }
            model.Province = GetProvincereList();
            model.City = GetCityList(model.ProvinceID);
            model.District = GetDistrictList(model.CityID);

            return View(model);    
            
        }

        public JsonResult GetBZ(string target, long id)
        {
            var list = new List<IList<SelectListItem>>();
            if (target == "Province")
            {
                var clist = GetCityList(id);
                var dlist = GetDistrictList(id);
                list.Add(clist);
                list.Add(dlist);
                return Json(list, JsonRequestBehavior.AllowGet);
            }
            else if (target == "City")
            {
                var dlist = GetDistrictList(id);
                list.Add(dlist);
                return Json(list, JsonRequestBehavior.AllowGet);
            }
            return null;
          
        }

        public IList<SelectListItem> GetProvincereList(long id = -1)
        {
            var list = CityService.GetProvincere().ToSelectListItem(new SelectListItem() {Text = "请选择省", Value = "-1"});
            return list;
        }

        public  IList<SelectListItem> GetCityList(long id = -1)
        {
            var clist = id == -1 ? (new List<S_City>()).ToSelectListItem(new SelectListItem() { Selected = true, Text = "请选择城市", Value = "-1" })
                                  : CityService.GetCity(id).ToSelectListItem(new SelectListItem() { Selected = true, Text = "请选择城市", Value = "-1" });
            return clist;
        }

        public IList<SelectListItem> GetDistrictList(long id = -1)
        {
            var list = id == -1 ? (new List<S_District>()).ToSelectListItem(new SelectListItem() { Selected = true, Text = "请选择区", Value = "-1" })
                    : CityService.GetDistrict(id).ToSelectListItem(new SelectListItem() { Selected = true, Text = "请选择城市", Value = "-1" });
            return list;
        }
   }
}
